package com.haoan.index.service.impl;


import java.util.List;
import javax.annotation.Resource;

import com.haoan.index.domain.AnnualPlace;
import com.haoan.index.domain.Personnel;
import com.haoan.index.mapper.PersonnelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.IntroduceMapper;
import com.haoan.index.domain.Introduce;
import com.haoan.index.service.IntroduceService;

/**
 * 企业文化Service业务层处理
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Service
public class IntroduceServiceImpl extends ServiceImpl<IntroduceMapper,Introduce> implements IntroduceService
{
    @Resource
    private PersonnelMapper personnelMapper;
    /**
     * 查询企业文化
     *
     * @param id 企业文化主键
     * @return 企业文化
     */
    @Override
    public Introduce selectIntroduceById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询企业文化列表
     *
     * @param introduce 企业文化
     * @return 企业文化
     */
    @Override
    public List<Introduce> selectIntroduceList(Introduce introduce)
    {
        LambdaQueryWrapper<Introduce> queryWrapper = new LambdaQueryWrapper<>();
        //todo 添加查询条件
        queryWrapper.eq(introduce.getId() != null, Introduce::getId, introduce.getId());
        queryWrapper.eq(introduce.getPicture() != null, Introduce::getPicture, introduce.getPicture());
        queryWrapper.eq(introduce.getIntroduce() != null, Introduce::getIntroduce, introduce.getIntroduce());
        queryWrapper.eq(introduce.getIntroduce1() != null, Introduce::getIntroduce1, introduce.getIntroduce1());
        queryWrapper.eq(introduce.getIntroduce2() != null, Introduce::getIntroduce2, introduce.getIntroduce2());
        queryWrapper.eq(introduce.getIntroduce3() != null, Introduce::getIntroduce3, introduce.getIntroduce3());
        queryWrapper.eq(introduce.getCreateTime() != null, Introduce::getCreateTime, introduce.getCreateTime());
        queryWrapper.eq(introduce.getCreateBy() != null, Introduce::getCreateBy, introduce.getCreateBy());
        queryWrapper.eq(introduce.getUpdateTime() != null, Introduce::getUpdateTime, introduce.getUpdateTime());
        queryWrapper.eq(introduce.getUpdateBy() != null, Introduce::getUpdateBy, introduce.getUpdateBy());
        queryWrapper.eq(Introduce::getValiFlag,ValiFlag.VALID );
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增企业文化
     *
     * @param introduce 企业文化
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIntroduce(Introduce introduce)
    {
        baseMapper.insert(introduce);
        if (null != introduce.getPersonnelList()){
            for (Personnel personnel : introduce.getPersonnelList()) {
                personnel.setIntroduceId(introduce.getId());
                personnelMapper.insert(personnel);
            }
        }
        return 1;
    }

    /**
     * 修改企业文化
     *
     * @param introduce 企业文化
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIntroduce(Introduce introduce)
    {
        LambdaQueryWrapper<Personnel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Personnel::getIntroduceId,introduce.getId());
        personnelMapper.delete(queryWrapper);
        baseMapper.updateById(introduce);
        if (null != introduce.getPersonnelList()){
            for (Personnel personnel : introduce.getPersonnelList()) {
                personnel.setIntroduceId(introduce.getId());
                personnelMapper.insert(personnel);
            }
        }
        return 1;
    }

    /**
     * 批量删除企业文化
     *
     * @param ids 需要删除的企业文化主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIntroduceByIds(Long [] ids)
    {
        Introduce introduce = new Introduce();
        introduce.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Introduce> updQw = new LambdaQueryWrapper<>();
        updQw.in(Introduce::getId, ids);
        return baseMapper.update(introduce, updQw);
    }

    /**
     * 删除企业文化信息
     *
     * @param id 企业文化主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Introduce introduce = getById(id);
        introduce.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(introduce);
    }
}
